package com.pxxy.mall.member.service.impl;

import com.pxxy.common.exception.BizCodeEnum;
import com.pxxy.common.utils.R;
import com.pxxy.mall.member.dao.MemberLevelDao;
import com.pxxy.mall.member.entity.MemberLevelEntity;
import com.pxxy.mall.member.vo.MemberLoginVo;
import com.pxxy.mall.member.vo.MemberRegistVo;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxxy.common.utils.PageUtils;
import com.pxxy.common.utils.Query;

import com.pxxy.mall.member.dao.MemberDao;
import com.pxxy.mall.member.entity.MemberEntity;
import com.pxxy.mall.member.service.MemberService;
import org.springframework.util.StringUtils;


@Service("memberService")
@AllArgsConstructor
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    private MemberLevelDao memberLevelDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R regist(MemberRegistVo memberRegistVo) {

        Long username = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().lambda().eq(MemberEntity::getUsername, memberRegistVo.getUserName()));
        if (username > 0) {
            return R.error().put("data", "用户名已存在");
        }
        Long phone = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().lambda().eq(MemberEntity::getMobile, memberRegistVo.getPhone()));

        if (phone > 0) {
            return R.error().put("data", "手机号已存在，请确认");
        }
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setMobile(memberRegistVo.getPhone());
        memberEntity.setUsername(memberRegistVo.getUserName());

        //md5密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(memberRegistVo.getPassword());
        memberEntity.setPassword(encode);

        //初始化默认会员等级
        MemberLevelEntity memberLevelEntity = memberLevelDao.selectOne(new QueryWrapper<MemberLevelEntity>().lambda().eq(MemberLevelEntity::getDefaultStatus, 1L));
        memberEntity.setLevelId(memberLevelEntity.getId());

        this.baseMapper.insert(memberEntity);
        return R.ok();

    }

    @Override
    public R login(MemberLoginVo memberLoginVo) {
        MemberEntity member = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().lambda()
                .eq(MemberEntity::getUsername, memberLoginVo.getLoginAccount())
                .or()
                .eq(MemberEntity::getMobile, memberLoginVo.getLoginAccount())
                .or()
                .eq(MemberEntity::getEmail, memberLoginVo.getLoginAccount()));
        if (!StringUtils.isEmpty(member)) {
            //md5验证
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(memberLoginVo.getPassword(), member.getPassword());
            if (matches) {
                return R.ok().put("user", member);
            } else {
                return R.error(BizCodeEnum.LOGIN_ERROR_EXCEPTION.getCode(), BizCodeEnum.LOGIN_ERROR_EXCEPTION.getMsg());
            }
        } else {
            return R.error(BizCodeEnum.LOGIN_ERROR_EXCEPTION.getCode(), BizCodeEnum.LOGIN_ERROR_EXCEPTION.getMsg());
        }
    }

}